<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/2
 * Time: 20:38
 */

namespace app\admin\controller;
use think\Db;
use think\Loader;
use think\Request;
use app\admin\model\Baidu;

class CasesCat extends Admin
{
    /**
     * 分类列表
     */
    public function lst()
    {
        $article_cat = model('CasesCat');
        $data =  $article_cat->select()->toArray();
        $article_cat_data =  $article_cat->tree($data);
        $this->assign('article_cat_data', $article_cat_data);
        return $this->fetch();
    }

    /**
     * 添加分类
     */
    public function add()
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('CasesCat');
            $articleCat = input('post.');
            // 接收并验证表单
            if($validate->check($articleCat))
            {
                $res = Db::name('cases_cat')->insert($articleCat);
                if($res)
                {
                    //主动推送链接到百度收录
                    $id = Db::name('cases_cat')->getLastInsID();
                    $urls = url('home/cases/index',['cid'=>$id],true,true);
                    $baiduModel = new Baidu();
                    $baiduModel->sendUrl($urls);
                    $this->success('添加成功', url('lst'));
                }
                else
                {
                    $this->success('添加失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }

        $article_cat =  model('CasesCat');
        $data =  $article_cat->select();
        $article_cat_data =  $article_cat->tree($data);
        $this->assign('article_cat_data', $article_cat_data);
        return $this->fetch();
    }


    public  function save($cat_id)
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('CasesCat');
            $articleCat = input('post.');
            // 接收并验证表单
            if($validate->check($articleCat))
            {
                $res = Db::name('cases_cat')->update($articleCat);
                if($res)
                {
                    $this->success('修改成功', url('lst'));
                }
                else
                {
                    $this->success('修改失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        // 取出要修改的记录
        $article_cat_model = model('CasesCat');
        $article_cat_data  = $article_cat_model->find($cat_id);
        //取出下拉数据
        $data =  $article_cat_model->field("cat_id,cat_name,parent_id")->select();
        $article_cat =  $article_cat_model->tree($data);
        $this->assign('article_cat',$article_cat);
        $this->assign('article_cat_data', $article_cat_data);
        return $this->fetch();
    }


    public function delete($cat_id)
    {
        $article_cat = new \app\admin\model\CasesCat();
        $data =  $article_cat->field('cat_id,cat_name,parent_id')->select();
        $article_cat_data =  $article_cat->getAllSubCatByParentId($data,$cat_id);
        if(!empty($article_cat_data))
        {
            $this->error("该类下有子类，先删除子类");
        }
        else
        {
            db('cases_cat')->delete($cat_id);
            //删除该子类下的所有文章
            db('cases')->where(array('cat_id'=>$cat_id))->delete();
            $this->success('成功删除该分类以及分类下的案例！');
        }
    }




}